#include "defined.h"
#include "uart_tcp.h"
#include "string.h"
u8 TCP_RX_BUF[RCV_TCP_BUF_SIZE],Tcp_Rx_BufBak[RCV_TCP_BUF_SIZE];//接收缓冲，发送缓冲

//--------------发送一个字符 -----------------------
void UART_SendByte(USART_TypeDef* USARTx,u16 Data)  
{ 
	USART_SendData(USARTx, Data); 
	while( USART_GetFlagStatus(USARTx,USART_FLAG_TC)!= SET);  
	//while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);//发送寄存器空,已将数据转移到移位寄存器   ---跳出
}

//===============================crc16计算============================================
const unsigned char auchCRCHi[] = 
{
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
	0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
	0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
	0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
	0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
	0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
	0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
	0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40
};


/*低位字节表Table of CRC values for low  order byte */
const unsigned char auchCRCLo[] = 
{
	0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
	0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
	0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
	0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0x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D, 0x4D, 0x4C, 0x8C,
	0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
	0x40
};

unsigned CRC16_result(u8 *puchMsg, u8 uchDataLen)
{
	unsigned char uchCRCHi = 0xFF; 	/* 初始化高字节 */
	unsigned char uchCRCLo = 0xFF; 	/* 初始化低字节 */
	u8 uIndex ; 				/*CRC表 */

	while(uchDataLen--)   		/* 通过数据缓冲器 */ 
	{
		uIndex = uchCRCLo ^ *puchMsg++ ; 	/* 计算CRC */
		uchCRCLo = uchCRCHi ^ auchCRCHi[uIndex];
		uchCRCHi = auchCRCLo[uIndex];
	}
	return (uchCRCHi << 8 | uchCRCLo);
}

#if DEBUG == -1
//---------------------uart-To-TCP---------------------------------
u8 TCP_RX_BUF[RCV_TCP_BUF_SIZE],Tcp_Rx_BufBak[RCV_TCP_BUF_SIZE];//接收缓冲，发送缓冲
TCP_CTL_STR	TcpCtl;   //写下行
TCP_READ_STR TcpRead;//读下行
TCP_CTL_POST_STR 	TcpCtl_Post;		//写控制帧正确上传
RE_TCP_TYPE			Re_Tcp_Type;	//接收状态类型
TCP_FAILURE_STR TcpErr;//错误接收
extern u8  IR_postflag,MT_postflag,QR_postflag,Alarm_postflag,DL_postflag,CtlErrflag,ScanErrflag,CtlOKflag,ScanOKflag;//操控OK，读状态OK

extern u8 testflag,testk;
extern u8 rxtest[20];
extern MT_POST_STR    	Post_Info,Post_InfoBAK;//上传信息
extern u16	uart_over_timecnt;
extern u8 CMDnode,SCANnode;//操控指令标识
extern SOCKET_AT_TYPE   Tx_Ready_Node; 
extern SOCKET_AT_TYPE  Bak_Ready_Node[10];//指令包
extern u16  tx_Read_timebase;//循环读计时
extern  TCP_Rx_STR  TcpStr[6];//接收数据

static char  TcpRvcomp='0';
//------------------TCP模块中断接收---------------------------------------
static  signed char TCP_RX_cnt=0,TCP_cntBak=0;


//操控发送
void UART_TCP_CTL(u16 addr,u16 data )
{
	GPIO_SetBits(GPIOA, GPIO_Pin_11);
	
	TcpCtl.Addr=TCP_ADDR_COMM;
	TcpCtl.Func =TCP_WRITE_COMM;
	TcpCtl.C_Addr.intData=addr;
	TcpCtl.C_Num.intData=1;//长度均为1
	TcpCtl.C_Addr=BIG2SMALL(&TcpCtl.C_Addr);
	TcpCtl.C_Num=BIG2SMALL(&TcpCtl.C_Num);
	TcpCtl.C_Data.intData=data;
	TcpCtl.C_Data=BIG2SMALL(&TcpCtl.C_Data);	
	TcpCtl.Crc16.intData=CRC16_result(&TcpCtl.Addr,(Rv_Wd_TCP_LEGTH-2));//计算crc16	
	Slave_Cmd_Send(USART2,&TcpCtl.Addr,(Rv_Wd_TCP_LEGTH-2));
	Slave_afantiU_Send(USART2,&TcpCtl.Crc16);//发送crc16 	
	delay_us(50);
	Slave_Cmd_Send(USART1,&TcpCtl.Addr,(Rv_Wd_TCP_LEGTH-2));
	Slave_afantiU_Send(USART1,&TcpCtl.Crc16);//发送crc16 	
	uart_over_timecnt=3500;	
	delay_us(800);
	GPIO_ResetBits(GPIOA, GPIO_Pin_11);
}

//每100ms循环读指令
void UART_TCP_AutoTx()
{
	if(TCP_RX_cnt!=0)   return;//接收中返回
	GPIO_SetBits(GPIOA, GPIO_Pin_11);
	TcpRead.Addr=TCP_ADDR_COMM;
	TcpRead.Func =TCP_READ_COMM;
	TcpRead.Start_Addr.intData=0x000;
	TcpRead.Number.intData=TCP_MAX_POST;
	TcpRead.Start_Addr=BIG2SMALL(&TcpRead.Start_Addr);
	TcpRead.Number=BIG2SMALL(&TcpRead.Number);
	TcpRead.Crc16.intData=CRC16_result(&TcpRead.Addr,(Rv_Rd_TCP_LEGTH-2));//计算crc16	
	Slave_Cmd_Send(USART2,&TcpRead.Addr,(Rv_Rd_TCP_LEGTH-2));//发送数据	 //注意大小端,此处不可直接发送整体			
	Slave_afantiU_Send(USART2,&TcpRead.Crc16);//发送crc16 
	delay_us(50);
	Slave_Cmd_Send(USART1,&TcpRead.Addr,(Rv_Rd_TCP_LEGTH-2));
	Slave_afantiU_Send(USART1,&TcpRead.Crc16);//发送crc16 	
	delay_us(800);
	GPIO_ResetBits(GPIOA, GPIO_Pin_11);
}
//TCP 处理函数  1ms
void UART_TCP_TxEvent()
{	
	tx_Read_timebase++;//倒计时要一直加
	
	switch(Tx_Ready_Node)
	{
		case MT_AT_NULL: //无任务--定时查询-------			
			if(tx_Read_timebase>150)
			{//100ms
				tx_Read_timebase=0;
				UART_TCP_AutoTx();
			}			
			break;
		case MT_AT_FORWARD_P://正向开			
			if(tx_Read_timebase<10) break;//刚发送完一帧10ms内不发送下一帧防止粘包
			tx_Read_timebase=0;
			TcpStr[0].cmd=Tx_Ready_Node;
			TcpStr[0].state=1;
			TcpStr[0].ttl=2000;			
			CMDnode=Tx_Ready_Node;
			UART_TCP_CTL(0,CMDnode);
			Tx_Ready_Node=MT_AT_NULL;
			break;
		case MT_AT_REVERSE_N://反向开
			if(tx_Read_timebase<10) break;//刚发送完一帧10ms内不发送下一帧防止粘包
			tx_Read_timebase=0;
			TcpStr[1].cmd=Tx_Ready_Node;
			TcpStr[1].state=1;
			TcpStr[1].ttl=2000;
			CMDnode=Tx_Ready_Node;
			UART_TCP_CTL(1,CMDnode);
			Tx_Ready_Node=MT_AT_NULL;
			break;
		case MT_AT_CLOSE:	//关门
			if(tx_Read_timebase<10) break;//刚发送完一帧10ms内不发送下一帧防止粘包
			tx_Read_timebase=0;
			TcpStr[2].cmd=Tx_Ready_Node;
			TcpStr[2].state=1;
			TcpStr[2].ttl=2000;
			CMDnode=Tx_Ready_Node;
			UART_TCP_CTL(2,CMDnode);
			Tx_Ready_Node=MT_AT_NULL;
			break;
		case MT_AT_KEEP_MOD: //常开
			if(tx_Read_timebase<10) break;//刚发送完一帧10ms内不发送下一帧防止粘包
			tx_Read_timebase=0;
			TcpStr[3].cmd=Tx_Ready_Node;
			TcpStr[3].state=1;
			TcpStr[3].ttl=2000;
			CMDnode=Tx_Ready_Node;
			UART_TCP_CTL(3,CMDnode);
			Tx_Ready_Node=MT_AT_NULL;
			break;
		case MT_AT_KEEP_CANEL:	//取消常开
			if(tx_Read_timebase<10) break;//刚发送完一帧10ms内不发送下一帧防止粘包
			tx_Read_timebase=0;
			TcpStr[4].cmd=Tx_Ready_Node;
			TcpStr[4].state=1;
			TcpStr[4].ttl=2000;
			CMDnode=Tx_Ready_Node;
			UART_TCP_CTL(4,CMDnode);
			Tx_Ready_Node=MT_AT_NULL;
			break;
		case MT_AT_STA://查询状态	
			if(tx_Read_timebase<10) break;//刚发送完一帧10ms内不发送下一帧防止粘包
			tx_Read_timebase=0;
			TcpStr[5].cmd=Tx_Ready_Node;
			TcpStr[5].state=1;
			TcpStr[5].ttl=2000;
			SCANnode=Tx_Ready_Node;
			UART_TCP_CTL(5,SCANnode);
			Tx_Ready_Node=MT_AT_NULL;
			break;
		default:
			tx_Read_timebase=0;
			SCANnode=0;
			CMDnode=0;
			Tx_Ready_Node=MT_AT_NULL;
			break;	
	}
	
}
					


////////////////////////////////////////////////////////////////////////////////////////
u8 HEX2BCD(u8 hex_data)    //HEX转为BCD
{ 
    u8 temp; 
    temp=(hex_data/10*16 + hex_data%10); 
    return temp; 
} 

u8 BCD2HEX(u8 bcd_data)    //BCD转为HEX
{ 
    u8 temp; 
    temp=(bcd_data/16*10 + bcd_data%16); 
    return temp; 
} 

//------------变量大端变小端--------------
//modus先传输高位需颠倒
afantiU BIG2SMALL(afantiU *Crcu16)
{
	u8 temp;
	temp=(*Crcu16).charData[0];
	(*Crcu16).charData[0]=(*Crcu16).charData[1];
	(*Crcu16).charData[1]=temp;
	return  *Crcu16;
}


//------------从机指令发送函数--------------------
void Slave_Cmd_Send(USART_TypeDef* USARTx ,u8*CmdStr,u8 length)
{
	u8 t;
	for(t=0;t<length;t++)
	{
		UART_SendByte(USARTx,*CmdStr++);
	}		
}
//------------从机发送一个afantiU函数----- 低在前,高在后---------------
void Slave_afantiU_Send(USART_TypeDef* USARTx ,afantiU* afantiU16)
{
	Slave_Cmd_Send(USARTx ,&(*afantiU16).charData[0],1);
	Slave_Cmd_Send(USARTx ,&(*afantiU16).charData[1],1);
}



///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//////////////////////485//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
#define PC485_Read		GPIO_ResetBits(GPIOA, GPIO_Pin_11)
#define PC485_Write		GPIO_SetBits(GPIOA, GPIO_Pin_11)

//---------------------485-To-TCP---------------------------------
u8 Tcp_Rx_BufBak_485[RCV_TCP_BUF_SIZE];//接收缓冲，发送缓冲
u8 Tcp_485_Flag=0;
//------------------TCP模块中断接收---------------------------------------
static  signed char TCP_cntBak_485=0;
void USART1_IRQHandler(void)
{
		u8 Res;
		u8 Clear=Clear;
		u16 val;

    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收一字节
    {
      USART_ClearFlag(USART1,USART_FLAG_RXNE);  
      USART_ClearITPendingBit(USART1, USART_IT_RXNE);   			
	  Res=USART_ReceiveData(USART1);	
      switch(TCP_cntBak_485)	
			{
				 case 0://ID		
							if(Res==TCP_ADDR_COMM)
									Tcp_Rx_BufBak_485[TCP_cntBak_485++]=Res; 
							else
									TCP_cntBak_485=0;
							break;						
					case 1://CH						
					case 2://addr
					case 3:
					case 4://data
					case 5:	
					case 6://crc16
					case 7:	
							Tcp_Rx_BufBak_485[TCP_cntBak_485++]=Res; 
						break;
					default:
							if(Tcp_Rx_BufBak_485[1]==TCP_READ_COMM)//0x04应答控制 
									Tcp_Rx_BufBak_485[TCP_cntBak_485++]=Res; 
							else   	
									TCP_cntBak_485=0;					
						break;
			}
		}else if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) //接受完一帧
		{		
				Clear=USART1->SR;//读SR寄存器
				Clear=USART1->DR;//读DR寄存器	(先读SR再读DR,为了清除IDLE标识)	
			
				if(TCP_cntBak_485!=0)
				{		
						TcpRvcomp='Y';	
						Tcp_485_Flag =1;
						memcpy(TCP_RX_BUF,Tcp_Rx_BufBak_485,TCP_cntBak_485);
						TCP_RX_cnt=TCP_cntBak_485;
						TCP_cntBak_485=0;
				}						
		}
}


#endif



